<template>
  <div class="box">
    <el-row>
      <el-col :span="24">
        <el-card shadow="hover">
          <div slot="header" class="clearfix">
            <span>基础用法</span>
          </div>
          <el-row>
            <el-checkbox v-model="checked1">备选项</el-checkbox>
          </el-row>

        </el-card>
      </el-col>
      <el-col :span="24">
        <el-card shadow="hover">
          <div slot="header" class="clearfix">
            <span>禁用状态</span>
          </div>
          <el-row>
            <el-checkbox v-model="checked2" disabled>备选项1</el-checkbox>
            <el-checkbox v-model="checked3" disabled>备选项</el-checkbox>
          </el-row>

        </el-card>
      </el-col>
      <el-col :span="24">
        <el-card shadow="hover">
          <div slot="header" class="clearfix">
            <span>多选框组</span>
          </div>
          <el-row>
            <el-checkbox-group v-model="checkList">
              <el-checkbox label="复选框 A"></el-checkbox>
              <el-checkbox label="复选框 B"></el-checkbox>
              <el-checkbox label="复选框 C"></el-checkbox>
              <el-checkbox label="禁用" disabled></el-checkbox>
              <el-checkbox label="选中且禁用" disabled></el-checkbox>
            </el-checkbox-group>
          </el-row>

        </el-card>
      </el-col>
      <el-col :span="24">
        <el-card shadow="hover">
          <div slot="header" class="clearfix">
            <span>indeterminate 状态</span>
          </div>
          <el-row>
            <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选
            </el-checkbox>
            <div style="margin: 15px 0;"></div>
            <el-checkbox-group v-model="checkedCities" @change="handleCheckedCitiesChange">
              <el-checkbox v-for="city in cities" :label="city" :key="city">{{ city }}</el-checkbox>
            </el-checkbox-group>
          </el-row>

        </el-card>
      </el-col>
      <el-col :span="24">
        <el-card shadow="hover">
          <div slot="header" class="clearfix">
            <span>可选项目数量的限制</span>
          </div>
          <el-row>
            <el-checkbox-group
              v-model="checkedCities1"
              :min="1"
              :max="2">
              <el-checkbox v-for="city in cities1" :label="city" :key="city">{{city}}</el-checkbox>
            </el-checkbox-group>
          </el-row>

        </el-card>
      </el-col>
      <el-col :span="24">
        <el-card shadow="hover">
          <div slot="header" class="clearfix">
            <span>按钮样式</span>
          </div>
          <el-row>
            <el-checkbox-group v-model="checkboxGroup1" size="medium">
              <el-checkbox-button v-for="city in cities" :label="city" :key="city" >{{city}}</el-checkbox-button>
            </el-checkbox-group>
          </el-row>

        </el-card>
      </el-col>
      <el-col :span="24">
        <el-card shadow="hover">
          <div slot="header" class="clearfix">
            <span>带有边框</span>
          </div>
          <el-row>
            <el-checkbox v-model="checked4" label="备选项1" border></el-checkbox>
            <el-checkbox v-model="checked5" label="备选项2" border></el-checkbox>
          </el-row>

        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  name: 'checkbox',
  data() {
    const cityOptions = ['上海', '北京', '广州', '深圳']
    return {
      checked1: true,
      checked2: false,
      checked3: true,
      checkList: ['选中且禁用', '复选框 A'],
      checkAll: false,
      checkedCities: ['上海', '北京'],
      cities: cityOptions,
      isIndeterminate: true,
      checkedCities1: ['上海', '北京'],
      cities1: cityOptions,
      checkboxGroup1: ['上海'],
      checked4: true,
      checked5: false,
    }
  },
  methods: {
    handleCheckAllChange(val) {
      this.checkedCities = val ? cityOptions : []
      this.isIndeterminate = false
    },
    handleCheckedCitiesChange(value) {
      let checkedCount = value.length
      this.checkAll = checkedCount === this.cities.length
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.cities.length
    }
  }
}
</script>

<style scoped lang="scss">
.clearfix {
  font-size: 14px;
}

.el-col {
  margin-bottom: 20px;
}
</style>
